package cn.iocoder.yudao.module.product.service.emissionsource;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import java.util.*;
import cn.iocoder.yudao.module.product.controller.admin.emissionsource.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.emissionsource.EmissionSourceDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.product.convert.emissionsource.EmissionSourceConvert;
import cn.iocoder.yudao.module.product.dal.mysql.emissionsource.EmissionSourceMapper;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.*;


/**
 * 排放源 Service 实现类
 *
 * @author wcl
 */
@Service
@Validated
public class EmissionSourceServiceImpl implements EmissionSourceService {

    @Resource
    private EmissionSourceMapper emissionSourceMapper;

    @Override
    public Long createEmissionSource(EmissionSourceCreateReqVO createReqVO) {
        // 插入
        EmissionSourceDO emissionSource = EmissionSourceConvert.INSTANCE.convert(createReqVO);
        emissionSourceMapper.insert(emissionSource);
        // 返回
        return emissionSource.getId();
    }

    @Override
    public void updateEmissionSource(EmissionSourceUpdateReqVO updateReqVO) {
        // 校验存在
        validateEmissionSourceExists(updateReqVO.getId());
        // 更新
        EmissionSourceDO updateObj = EmissionSourceConvert.INSTANCE.convert(updateReqVO);
        emissionSourceMapper.updateById(updateObj);
    }

    @Override
    public void deleteEmissionSource(Long id) {
        // 校验存在
        validateEmissionSourceExists(id);
        // 删除
        emissionSourceMapper.deleteById(id);
    }

    private void validateEmissionSourceExists(Long id) {
        if (emissionSourceMapper.selectById(id) == null) {
            throw exception(EMISSION_SOURCE_NOT_EXISTS);
        }
    }

    @Override
    public EmissionSourceDO getEmissionSource(Long id) {
        return emissionSourceMapper.selectById(id);
    }

    @Override
    public List<EmissionSourceDO> getEmissionSourceList(Collection<Long> ids) {
        return emissionSourceMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<EmissionSourceDO> getEmissionSourcePage(EmissionSourcePageReqVO pageReqVO) {
        return emissionSourceMapper.selectPage(pageReqVO);
    }

}
